//
//  BaseCollectionViewVC.m
//  GasCome
//
//  Created by zfc on 2020/5/4.
//  Copyright © 2020 zfc. All rights reserved.
//

#import "BaseCollectionViewVC.h"

@interface BaseCollectionViewVC ()

@end

@implementation BaseCollectionViewVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;

    [self fetchData];
    [self.view addSubview:self.collectionView];
    [self addRefresh];

}

- (void) fetchData {
    [self endRefresh];
}

- (void) addRefresh {
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self.collectionView.mj_footer resetNoMoreData];
        [self fetchData];
    }];

    if (self.isAddFooter) {
        self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            self.page = self.page + 1;
            [self fetchData];
        }];
        self.collectionView.mj_footer.automaticallyHidden = YES;
    }
}

- (void) refresh {
    [self.collectionView.mj_header beginRefreshing];
}

- (void) endRefresh {
    [self.collectionView.mj_header endRefreshing];
    [self.collectionView.mj_footer endRefreshing];
}

- (void)setIsAddFooter:(BOOL)isAddFooter {
    if (isAddFooter) {
        self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            self.page = self.page + 1;
            [self fetchData];
        }];
        self.collectionView.mj_footer.automaticallyHidden = YES;
    }
}

- (void)setIsRemoveHeader:(BOOL)isRemoveHeader {
    if (isRemoveHeader) {
        self.collectionView.mj_header = nil;
    }
}

#pragma mark - lazy
- (UICollectionView *) collectionView{
    if (!_collectionView) {
        CGFloat itemW = (kScreenW-45)/2;
        _layout =  [[UICollectionViewFlowLayout alloc] init];
        _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _layout.headerReferenceSize = CGSizeMake(kScreenW, 0);
        _layout.footerReferenceSize = CGSizeMake(kScreenW, 0);

        _layout.itemSize = CGSizeMake(itemW, itemW);
        _layout.minimumLineSpacing = 0;
        _layout.minimumInteritemSpacing = 6;
        _layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);

        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.backgroundColor = WhiteColor;
        

    }
    return _collectionView;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 0;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ID" forIndexPath:indexPath];
    if (!cell) {
        cell = [[UICollectionViewCell alloc] init];
    }
    return cell;
}

- (NSMutableArray *)dataList {
    if (!_dataList) {
        _dataList = [NSMutableArray array];
    }
    return _dataList;
}

@end
